using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using DecentTasksReminder.TimerJob;

namespace DecentTasksReminder.Features.ReminderTasksFeature
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("64b534f9-6d95-4bfa-8228-a03767c13243")]
    public class ReminderTasksFeatureEventReceiver : SPFeatureReceiver
    {
        const string List_JOB_NAME = "DecentTasksListLogger";
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = null;
            SPWeb web = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    site = properties.Feature.Parent as SPSite;
                    site.AllowUnsafeUpdates = true;
                    web = site.RootWeb as SPWeb;

                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (job.Name == List_JOB_NAME)
                            job.Delete();
                    }

                    //install the openItems Timer Job
                    TasksReminderTimerJob openItemsTimerJob = new TasksReminderTimerJob(List_JOB_NAME, site.WebApplication);

                    //SPMinuteSchedule schedule = new SPMinuteSchedule();
                    SPDailySchedule schedule = new SPDailySchedule();

                    schedule.BeginHour = 05; //for example 23 for 11 pm consider taking 24 hours
                    schedule.BeginMinute = 00;
                    schedule.BeginSecond = 0;

                    schedule.EndHour = 05; //23 for 11 pm
                    schedule.EndMinute = 00;

                    schedule.EndSecond = 15;

                    openItemsTimerJob.Schedule = schedule;
                    openItemsTimerJob.Update();
                    site.AllowUnsafeUpdates = false;

                });
            }
            catch (Exception ex)
            {
                SPUtility.TransferToErrorPage(ex.Message);
            }
            finally
            {
                web.Dispose();
                site.Dispose();
            }
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = null;
            SPWeb web = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    site = properties.Feature.Parent as SPSite;
                    site.AllowUnsafeUpdates = true;
                    web = site.RootWeb as SPWeb;

                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (job.Name == List_JOB_NAME)
                            job.Delete();
                    }
                    site.AllowUnsafeUpdates = false;
                });
            }
            catch (Exception ex)
            {
                SPUtility.TransferToErrorPage(ex.Message);
            }
            finally
            {
                web.Dispose();
                site.Dispose();
            }
        }

        private static void DeleteJob(SPSite site)
        {
            //delete the job
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == List_JOB_NAME)
                {
                    job.Delete();
                    break;
                }
            }
        }
    }
}
